package cc.sheji.Service;


import cc.sheji.domain.Comment;
import cc.sheji.domain.CommentQueryCriteria;
import cc.sheji.domain.PictureCollection;
import cc.sheji.domain.User;
import cc.sheji.reposity.CommentRepository;
import cc.sheji.reposity.PictureCollectionRepository;
import cc.sheji.reposity.UserRepository;
import cc.sheji.util.QueryHelp;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CommentService {

    @Autowired
    CommentRepository commentRepository;
    @Autowired
    PictureCollectionRepository pictureCollectionRepository;

    @Autowired
    UserRepository userRepository;
    public Comment create(Comment entity) {
        entity.setId(null);
        entity.setUserId(StpUtil.getLoginIdAsLong());
        PictureCollection pictureCollection = pictureCollectionRepository.findById(entity.getPictureCollectionId()).get();
        Integer commentNum = pictureCollection.getCommentNum();
        if(commentNum==null)
            commentNum=0;
        pictureCollection.setCommentNum(commentNum+1);
        entity.setPUserId(pictureCollection.getUserId());
        User user = userRepository.findById(pictureCollection.getUserId()).get();
        Integer loveNum1 = user.getCommentNum();
        if(loveNum1==null)
            loveNum1=0;
        user.setCommentNum(loveNum1+1);
        userRepository.save(user);
        commentRepository.save(entity);
        return entity;
    }

    public Comment get(Long id) throws InterruptedException {
        Comment entity = commentRepository.findById(id).get();
        return entity;
    }

    public Comment update(Comment entity) {
        entity = commentRepository.save(entity);
        return entity;
    }

    @Transactional
    public boolean delete(Comment entity) {
//        System.out.println(entity.getPictureCollectionId());
//        commentRepository.deleteAllByPictureCollectionIdAndUserId(entity.getPictureCollectionId(), StpUtil.getLoginIdAsLong());
//        PictureCollection pictureCollection = pictureCollectionRepository.findById(entity.getPictureCollectionId()).get();
//        Integer loveNum = pictureCollection.getLoveNum();
//        if(loveNum==null)
//            loveNum=0;
//        pictureCollection.setLoveNum(loveNum-1);
//        pictureCollectionRepository.save(pictureCollection);
        return true;
    }

    public List<Comment> list(CommentQueryCriteria criteria, Pageable pageable) {
        Page<Comment> page = commentRepository.findAll((root, criteriaQuery, criteriaBuilder) ->
                QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return page.getContent();
    }

    public List<Comment> all(CommentQueryCriteria criteria, Pageable pageable) {
        Page<Comment> page = commentRepository.findAll((root, criteriaQuery, criteriaBuilder) ->
                QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        List<Comment> returnList=  new ArrayList<>();
        List<Comment> content = page.getContent();
        Map<Long,List<Comment>> map=new HashMap<>();
        for(Comment comment:content){
            Long parentId = comment.getParentId();
            if(parentId==null||parentId==0L){
                returnList.add(comment);
            }else {
                List<Comment> comments = map.get(parentId);
                if(comments==null){
                    comments=new ArrayList<>();
                    map.put(parentId,comments);
                }
                comments.add(comment);
            }
        }
        for(Comment comment:returnList){
            Long id = comment.getId();
            List<Comment> comments = map.get(id);
            if(comments==null) {
                comments = new ArrayList<>();
            }
            comment.setChild(comments);
        }
        return returnList;
    }
    public Map<String,Object> allPage(CommentQueryCriteria criteria, Pageable pageable) {
        Page<Comment> page = commentRepository.findAll((root, criteriaQuery, criteriaBuilder) ->
                QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        List<Comment> returnList=  new ArrayList<>();
        List<Comment> content = page.getContent();
        Map<Long,List<Comment>> map=new HashMap<>();
        for(Comment comment:content){
            Long parentId = comment.getParentId();
            if(parentId==null||parentId==0L){
            }else {
                List<Comment> comments = new ArrayList<>();
                comments.add(commentRepository.findById(parentId).get());
                comment.setChild(comments);
            }
            returnList.add(comment);
        }
        Map<String,Object>data=new HashMap<>();
        data.put("list",returnList);
        data.put("total",page.getTotalElements());
        return data;
    }
    public Map<String,Object> all1(CommentQueryCriteria criteria, Pageable pageable) {
        Page<Comment> page = commentRepository.findAll((root, criteriaQuery, criteriaBuilder) ->
                QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        List<Comment> returnList=  new ArrayList<>();
        List<Comment> content = page.getContent();
        Map<Long,List<Comment>> map=new HashMap<>();
        for(Comment comment:content){
            Long parentId = comment.getParentId();
            if(parentId==null||parentId==0L){
            }else {
                List<Comment> comments = new ArrayList<>();
                comments.add(commentRepository.findById(parentId).get());
                comment.setChild(comments);
            }
            returnList.add(comment);
        }
        for(Comment comment:returnList){
            Long id = comment.getId();
            List<Comment> comments = map.get(id);
            if(comments==null) {
                comments = new ArrayList<>();
            }
            comment.setChild(comments);
        }
        Map<String,Object>data=new HashMap<>();
        data.put("list",returnList);
        data.put("total",page.getTotalElements());
        return data;
    }
}
